/**
 * 检测管理器模块
 * 负责检测结果显示和处理
 */

class DetectionManager {
    constructor() {
        this.detectionOverlay = document.getElementById('detectionOverlay');
        this.init();
    }

    init() {
        // 初始化时隐藏检测覆盖层
        this.hideDetectionOverlay();
    }

    showDetectionResult(result) {
        const statusElement = document.getElementById('detectionStatus');
        const confidenceElement = document.getElementById('detectionConfidence');

        if (!statusElement || !confidenceElement || !this.detectionOverlay) {
            return;
        }

        if (result.status === 'defective') {
            statusElement.textContent = `缺陷: ${this.getDefectTypeName(result.type)}`;
            statusElement.style.color = '#f44336';
        } else {
            statusElement.textContent = '正常';
            statusElement.style.color = '#4CAF50';
        }

        confidenceElement.textContent = `${(result.confidence * 100).toFixed(1)}%`;
        this.detectionOverlay.style.display = 'block';

        // 3秒后隐藏
        setTimeout(() => {
            this.hideDetectionOverlay();
        }, 3000);
    }

    hideDetectionOverlay() {
        if (this.detectionOverlay) {
            this.detectionOverlay.style.display = 'none';
        }
    }

    getDefectTypeName(type) {
        const names = {
            'crazing': '开裂',
            'inclusion': '夹杂物',
            'patches': '斑点',
            'pitted_surface': '凹坑',
            'rolled-in_scale': '卷入',
            'scratches': '划痕',
        };
        return names[type] || type;
    }

    processDetectionResult(result) {
        // 显示检测结果
        this.showDetectionResult(result);
        
        // 添加日志
        if (window.logManager) {
            window.logManager.addLogEntry('detection', 
                `检测结果: ${result.status === 'defective' ? '缺陷' : '正常'} | ` +
                `类型: ${this.getDefectTypeName(result.type)} | ` +
                `置信度: ${(result.confidence * 100).toFixed(1)}%`
            );
        }
    }
}

// 导出到全局
window.DetectionManager = DetectionManager; 